package com.example.testbroadcast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    BroadcastReceiver mBr = new DynamicReceiver();
    //local broadcast
    private LocalBroadcastManager localBroadcastManager ;
    BroadcastReceiver mBrLocal = new DynamicReceiver();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.id_btn_static).setOnClickListener(this);
        findViewById(R.id.id_btn_dynamic).setOnClickListener(this);
        findViewById(R.id.id_btn_local).setOnClickListener(this);

        findViewById(R.id.id_register_dynamic).setOnClickListener(this);
        findViewById(R.id.id_unregister_dynamic).setOnClickListener(this);
        findViewById(R.id.id_btn_register_local).setOnClickListener(this);
        findViewById(R.id.id_unregister_local).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.id_btn_static:
                Intent intent = new Intent("helloReceiver1");
                intent.setComponent(new ComponentName(this.getPackageName(),"com.example.testbroadcast.MyReceiver"));
                sendBroadcast(intent);
                break;
            case R.id.id_btn_dynamic:
                LogUtil.printLog("case R.id.id_btn_dynamic:");
                intent = new Intent();
                intent.setAction(DynamicReceiver.BROADCAST_ACTION_DISC);
                //发送广播
                sendBroadcast(intent);
                break;
            case R.id.id_btn_local:
                intent = new Intent(DynamicReceiver.BROADCAST_ACTION_LOCAL) ;
                intent.putExtra( "data" , "主线程发过来的消息" ) ;
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent) ;
                break;
            case R.id.id_register_dynamic:
                LogUtil.printLog("case R.id.id_register_dynamic:");
                IntentFilter filter = new IntentFilter();
                filter.addAction(DynamicReceiver.BROADCAST_ACTION_DISC);
                this.registerReceiver(mBr, filter);
                break;
            case R.id.id_unregister_dynamic:
                LogUtil.printLog("case R.id.id_unregister_dynamic:");
                unregisterReceiver(mBr);
                break;
            case R.id.id_btn_register_local:
                //注册广播接收器
                IntentFilter intentFilter = new IntentFilter(DynamicReceiver.BROADCAST_ACTION_LOCAL) ;
                LocalBroadcastManager.getInstance(this).registerReceiver( mBrLocal , intentFilter );
                break;
            case R.id.id_unregister_local:
                LocalBroadcastManager.getInstance(this).unregisterReceiver(mBrLocal);
                break;
        }
    }
}
